# Figure_1.R
# (formerly divergence_vs_polarization.R)
#
# 2009 June 28
# John G. Bullock
#
# Creates PDF of Figure 1 in "Partisan Bias and the Bayesian Ideal
# in the Study of Public Opinion," Journal of Politics 71 (July): 
# 1109-24, http://dx.doi.org/10.1017/S0022381609090914.
#
# sessionInfo() output is at the bottom of this file.

if (any(regexpr('Hmisc', search())>-1)) { detach(package:Hmisc) }
library(grid)
library(lattice)


# PRELIMINARIES
filename.output <- 'Figure_1.pdf'
PDFtitle        <- 'Figure 1: Divergence vs. Polarization' 
PDFwidth        <- 6.75
PDFheight       <- 3.0
panelwidth      <- list(2.05, "inches")
panelheight     <- list(1.10, "inches") # .55 for three lines
betweenlist     <- list(y=1.2, x=2.225)                   
stripsize       <-  .75     # cex
xaxtextsize     <-  .8*.87  # cex
yaxtextsize     <-  .8*.87  # cex
xaxs            <- list(  draw=F, labels=c(1,4,7,10), at=c(2,5,8,11), tck=c(.75, 0), col="black", cex=xaxtextsize
                        , alternating=1, relation="same")
xaxs.lab.y      <- -.1
yaxs            <- list(draw=F, labels=0:8, at=0:8 # at=c(5, 10, 15, 20),
                        , tck=c(.75, 0), cex=yaxtextsize
                        , alternating=c(2,2), relation="same")
axiscolor       <- 'black'
labelcolor      <- 'black'
superposecol1   <- 'black'
superposecol2   <- 'black'


# SET UP COORDINATES FOR THE PLOTTED LINES
convergence.types.data <- data.frame(  y=c(4,8, 4.1,8, 3,5, 3.1,1), x=c(1,2,1,2,1,2,1,2)
                                       , type=c('x1','x1','x1','x1','x2','x2','x2','x2'), panel=c(1,1,2,2,1,1,2,2))


# PANEL FUNCTION
convergence.types.panel <- function(...) {
  panel.xyplot(...)
  trellis.par.set("clip", list(panel="off", strip="off")) # permits setting of text outside the panels
  
  # axis labels
  grid.text('t', x= .1, y=xaxs.lab.y, just='center', gp=gpar(font=3, cex=.8, col=labelcolor))
  grid.text('u', x= .9, y=xaxs.lab.y, just='center', gp=gpar(font=3, cex=.8, col=labelcolor))
  grid.text('belief means', x=-.05, y=0.5, vjust=.5, gp=gpar(font=1, cex=.8, col=labelcolor), rot=90)
  if (panel.number()==1)      { grid.text("divergence without polarization", x=.5, y=-.29, just="center", gp=gpar(font=2, cex=.8, col=labelcolor)) }
  else if (panel.number()==2) { grid.text("divergence with polarization",    x=.5, y=-.29, just="center", gp=gpar(font=2, cex=.8, col=labelcolor)) }
}


# MAKE THE PDF FILE
pdf(   file=filename.output, width=PDFwidth, height=PDFheight, paper="special", onefile=F, title=PDFtitle
    #, family=c("gn______.afm", "GNB_____.afm", "gni_____.afm", "GNBI____.afm", "sy______.afm")  # use Gill Sans -- if you have it
    )   
par(lend='square') # line endings
trellis.par.set("dot.symbol", list(alpha=1, cex=0, col="black", font=1, pch='x')) 
trellis.par.set("axis.line", list(alpha=1, col=axiscolor, lty=1, lwd=.1)) # to eliminate lattice panel border, set col="white"
trellis.par.set(superpose.line=list(alpha=c(1,1), col=c(superposecol1,superposecol2), lty=c(1,1), lwd=c(1,1)))
convergence.types.plot <- xyplot(  y ~ x | panel
                                 , data=convergence.types.data, type="l", lineend='square' 
                                 , groups=type 
                   				 , xlab="", ylab=""
                   			 	 , panel=convergence.types.panel
			                     , layout=c(2,1)                    # x columns, y rows
			                     , between=betweenlist
			                     , strip=FALSE
			                     , scales=list(x=xaxs, y=yaxs)
                                )
print(convergence.types.plot, panel.width=panelwidth, panel.height=panelheight) 
dev.off()
shell.exec(paste(getwd(), '/', filename.output, sep='')) # may only work in Windows

# > sessionInfo()
# R version 2.8.1 (2008-12-22) 
# i386-pc-mingw32 
#
# locale:
# 		LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
#
# attached base packages:
#		[1] grid      grDevices utils     datasets  graphics  stats     methods   base     
#
# other attached packages:
#		[1] lattice_0.17-25 car_1.2-12
